package com.sss.myspringsecurity.websocketcontroller;

import com.alibaba.fastjson.JSON;
import com.sss.myspringsecurity.config.websocketconfig.accuont.ResponseWebSocket;
import com.sss.myspringsecurity.config.websocketconfig.accuont.WebSocketServer;
import com.sss.myspringsecurity.config.websocketconfig.stomp.STOMPConnectEventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

/**
 * @author sss
 * @date 2020-02-16 12:27
 **/
@Controller
@RequestMapping("/websocket")
@CrossOrigin(origins = "*")
public class DemoController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private STOMPConnectEventListener stompConnectEventListener;

    /**
     * 跳转到websocket页面
     * @param
     * @return java.lang.String
     * @author SSS
    **/
    @RequestMapping("/abc")
    public String page(){
        return "websocket";
    }

    /**
     * 往指定用户发送消息
     * @param
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     * @author SSS
    **/
    @GetMapping("/index")
    public ResponseEntity<String> index(){
        try {
            ResponseWebSocket responseWebSocket = new ResponseWebSocket();
            responseWebSocket.setCode(new Random().nextInt(10000));
            responseWebSocket.setCheckHeart(new Random().nextInt(10000)+"sadadas");
            WebSocketServer.sendInfo(JSON.toJSONString(responseWebSocket),"admin");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok("请求成功");
    }




    @GetMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId) throws IOException {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",411);
        String string = JSON.toJSONString(hashMap);
        WebSocketServer.sendInfo(string,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }


    /**
     *
     *
     *********************STOMP***************************************************
     *
    **/


    /**
     *
     * 跳转到stomp页面
     * @return java.lang.String
     * @author WYY-SSS
    **/
    @RequestMapping("/stomp")
    public String stomp(){
        return "stomp";
    }


    @MessageMapping("/hello")
    //发送到订阅频道
    @SendTo("/topic/greetings")
    public HelloMessage greeting(HelloMessage message ) throws Exception {
        String messageName = message.getName();
        return message;
    }

    @GetMapping("/oneself/{username}")
    public String sendCustomMessage(@PathVariable String username){
        String s = UUID.randomUUID().toString().replace("-", "");
        s+="啊啊大大";
        HelloMessage helloMessage = new HelloMessage();
        helloMessage.setMessage(s);
        String jsonString = JSON.toJSONString(helloMessage);
        simpMessagingTemplate.convertAndSendToUser(username,"/one",jsonString);
        return "websocket";
    }
}
